import request from '@/utils/request'

export interface LoginParams {
  userName: string
  accountId: string
  password: string
}

export interface RegisterParams extends LoginParams {
  role: string
}

export interface LoginResponseData {
  token: string
  role: string
  userName: string
  accountId: string
}

export interface UserInfo {
  id: string
  accountId: string
  userName: string
  role: string
}

export interface RecoverPasswordParams {
  id: string
  password: string
}

export interface ApiResponse<T> {
  code: number
  data: T
  msg: string
}

export const login = (data: LoginParams) => {
  return request<ApiResponse<LoginResponseData>>({
    url: '/api/auth/login',
    method: 'post',
    data,
  })
}

export const register = (data: RegisterParams) => {
  return request<ApiResponse<LoginResponseData>>({
    url: '/api/auth/register',
    method: 'post',
    data,
  })
}

export const forgotPassword = (data: { accountId: string }) => {
  return request<ApiResponse<any>>({
    url: '/api/auth/forgot-password',
    method: 'post',
    data,
  })
}

// 根据账号获取用户信息
export const getByAccountId = (accountId: string) => {
  return request<ApiResponse<UserInfo>>({
    url: `/api/user/getByAccountId/${accountId}`,
    method: 'get',
  })
}

// 重置密码
export const recoverPassword = (data: RecoverPasswordParams) => {
  return request<ApiResponse<null>>({
    url: '/api/user/recoverPassword',
    method: 'post',
    data,
  })
}
